from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import random  # 随机等待

def random_sleep(t: float=1.0):
    """
    随机等待 t 秒到 t+1 秒。

    参数:
    t (float): 最小等待时间
    """
    time.sleep(t + random.random())


def select_bind_button(button_name: str="全部"):
    """
    根据输入的按钮名称 点击是否绑定wegame/taptap相应的按钮。

    参数:
    button_name (str): 可选值为 "全部", "是", "否"
    """
    allowed_buttons = ["全部", "是", "否"]
    if button_name not in allowed_buttons:
        raise ValueError(f"Bind WeGame/TapTap: 无效的按钮名称={button_name}")
    
    button = driver.find_element(By.XPATH, f"//div[text()='{button_name}']")
    button.click()  # 点击找到的按钮
    print(f"Bind WeGame/TapTap: 成功点击 '{button_name}' 按钮.")


def select_system_button(button_name: str = "官服"):
    """
    根据输入的系统名称 点击相应的系统按钮。

    参数:
    button_name (str): 可选值为 "官服", "哔哩哔哩", "UID靓号", "小米服", "华为服", "OPPO服", "魅族服", "国际服"
    """
    # 允许的按钮名称
    allowed_buttons = [
        "官服",
        "哔哩哔哩",
        "UID靓号",
        "小米服",
        "华为服",
        "OPPO服",
        "魅族服",
        "国际服",
    ]

    # 检查按钮名称是否有效
    if button_name not in allowed_buttons:
        raise ValueError(f"Select System Button: 无效的按钮名称={button_name}")

    # 根据按钮名称选择相应的按钮并点击
    button = driver.find_element(By.XPATH, f"//div[text()='{button_name}']")
    button.click()  # 点击找到的按钮

    # 输出点击结果
    print(f"Select System Button: 成功点击 '{button_name}' 按钮.")


def select_gender_button(button_name: str = "全部"):
    """
    根据输入的性别或全部选择按钮点击相应的按钮。

    参数:
    button_name (str): 可选值为 "全部", "男主", "女主"
    """
    # 允许的按钮名称
    allowed_buttons = ["全部", "男主", "女主"]

    # 检查按钮名称是否有效
    if button_name not in allowed_buttons:
        raise ValueError(f"Select Gender Button: 无效的按钮名称={button_name}")

    button = driver.find_element(By.XPATH, f"//div[text()='{button_name}']")
    button.click()  # 点击找到的按钮

    # 输出点击结果
    print(f"Select Gender Button: 成功点击 '{button_name}' 按钮.")


# def select_account_type(checkbox_name: str = "初始号"):
#     """
#     根据输入的复选框名称，点击相应的复选框进行勾选。

#     参数:
#     checkbox_name (str): 可选值为 "初始号", "顶级账号", "诚心卖"
#     """
#     # 允许的复选框名称
#     allowed_checkboxes = ["初始号", "顶级账号", "诚心卖"]

#     # 检查复选框名称是否有效
#     if checkbox_name not in allowed_checkboxes:
#         raise ValueError(f"Select Checkbox: 无效的复选框名称={checkbox_name}")

#     # 定位并点击相应的复选框
#     checkbox = driver.find_element(
#         By.XPATH, f"//div[text()='{checkbox_name}']"
#     )
#     checkbox.click()  # 点击复选框的`div`，模拟勾选

#     # 输出点击结果
#     print(f"Select Account Type: 成功点击 '{checkbox_name}' 复选框.")


def click_allowed_checkboxes(allowed_checkboxes):
    """
    点击允许的复选框。

    :param driver: Selenium WebDriver对象
    :param allowed_checkboxes: 允许的复选框名称列表
    """
    # 遍历所有的复选框
    checkboxes = driver.find_elements(By.CSS_SELECTOR, "div.group")

    for checkbox in checkboxes:
        # 获取复选框旁边的文本内容
        checkbox_text = checkbox.text.strip()

        # 判断复选框名称是否在允许的列表中
        if checkbox_text in allowed_checkboxes:
            # 点击复选框
            checkbox.click()
            print(f"点击了复选框: {checkbox_text}")


# 测试：点击 "顶级账号" 复选框


driver = webdriver.Chrome("./chromedriver-win64/chromedriver.exe")
url = "https://www.pxb7.com/buy/10302/1?keyword=&searchType=%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2"
driver.get(url)  # 不需要验证用户就可以查找
driver.maximize_window()
random_sleep()  # 等待网页加载

# 测试按钮
select_gender_button("女主")
# select_account_type("诚心卖")
click_allowed_checkboxes("诚心卖")


time.sleep(1000)
